//
//  HRReDianViewController.m
//  Economical report
//
//  Created by wyzc on 16/6/23.
//  Copyright © 2016年 叶落知秋. All rights reserved.
//
#define CELLID @"CELL"
#import "HRReDianViewController.h"
#import <AFNetworking.h>
#import "HRReDianModel.h"
#import "HRReDianCell.h"
#import "UIImageView+WebCache.h"

@interface HRReDianViewController ()<UITableViewDelegate,UITableViewDataSource,UIScrollViewDelegate>
@property(nonatomic,strong)NSMutableArray *dataSourceArr;
@property (weak, nonatomic) IBOutlet UITableView *tableView;
@property(weak,nonatomic)UIScrollView *scrollview;
@property(weak,nonatomic)UIView *headView;
@property(weak,nonatomic)UIPageControl *pageControl;
@end

@implementation HRReDianViewController
-(NSMutableArray *)dataSourceArr
{
    if (_dataSourceArr==nil) {
        _dataSourceArr=[NSMutableArray array];
    }
    return _dataSourceArr;
}

-(void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    //获得x轴的偏移量
    CGFloat x=self.scrollview.contentOffset.x;
    //获得第几页
    int page=x/self.scrollview.frame.size.width;
    //把当前页赋值给分页控制器
    self.pageControl.currentPage=page;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    UIView *headView=[[UIView alloc]initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, 150)];
    self.headView = headView;
    self.tableView.tableHeaderView=self.headView;
    UIPageControl *pageControl=[[UIPageControl alloc] initWithFrame:CGRectMake(self.scrollview.frame.size.width/2, 140, 30, 20)];
    self.pageControl=pageControl;
    self.tableView.rowHeight=80;
    [self addXib];
    [self sendGetRequest];
    
    self.scrollview.delegate=self;
    
}
//注册xib
-(void)addXib
{
    UINib *nib=[UINib nibWithNibName:@"HRReDianCell" bundle:nil];
    [self.tableView registerNib:nib forCellReuseIdentifier:CELLID];
    
}
//从网络请求数据
-(void)sendGetRequest
{
    NSURLSession *session=[NSURLSession sharedSession];
    NSURL *url=[NSURL URLWithString:@"http://api.21jingji.com/app/headline?page=1"];
    NSURLSessionDataTask *dataTask=[session dataTaskWithURL:url completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
        NSDictionary *catNameDict=[NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableLeaves error:nil];
        NSArray *arr=catNameDict[@"list"];
        for (NSDictionary *dict in arr) {
            HRReDianModel *model=[HRReDianModel modelWithDict:dict];
            [self.dataSourceArr addObject:model];
        }
       [[ NSOperationQueue mainQueue]addOperationWithBlock:^{
           [self setHeader];
           [self.tableView reloadData];
       }];
        
        
    }];
    [dataTask resume];
}
//轮播图
-(void)setHeader
{
    UIScrollView *scrollview=[[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 150)];
    self.scrollview=scrollview;
    
    [self.headView addSubview:scrollview];
   
    CGFloat imgViewW=self.scrollview.frame.size.width;
    CGFloat imgViewH=self.scrollview.frame.size.height;
    CGFloat imgViewY=0;
    
    UIImageView *imgView=[[UIImageView alloc] initWithFrame:CGRectMake(0, 0, self.headView.frame.size.width, 150)];
    imgView.image=[UIImage imageNamed:@"topnews_shadow"];
    
    
    
    
    for (int i=0; i<3; i++) {
        HRReDianModel *model=self.dataSourceArr[i];
    
        UIImageView *imgView=[[UIImageView alloc] init];
        NSURL *url=[NSURL URLWithString:model.list_thumb];
        [imgView sd_setImageWithURL:url placeholderImage:nil];
        //创建titleLabel
        UILabel *titleLabel=[[UILabel alloc] initWithFrame:CGRectMake(10, 10, self.headView.frame.size.width-20, 20)];
        [self.headView addSubview:imgView];
        titleLabel.text=model.title;
        [imgView addSubview:titleLabel];
        //imgview位置及坐标
        CGFloat imgViewX=i*imgViewW;
        imgView.frame=CGRectMake(imgViewX, imgViewY, imgViewW, imgViewH);
        [self.scrollview addSubview:imgView];
        
    }
    //滚得范围
    self.scrollview.contentSize=CGSizeMake(3*imgViewW, 0);
    self.scrollview.showsHorizontalScrollIndicator=NO;
    //分页
    self.scrollview.pagingEnabled=YES;
    //设置分页指示器的总页数
    self.pageControl.numberOfPages=3;
    //设置当前页的颜色
    self.pageControl.currentPageIndicatorTintColor=[UIColor redColor];
    //设置其他页的颜色
    self.pageControl.pageIndicatorTintColor=[UIColor blueColor];
    [self.headView addSubview:self.pageControl];
    
    
    //设置定时器
    
    [NSTimer scheduledTimerWithTimeInterval:2 target:self selector:@selector(time) userInfo:nil repeats:YES];
}
-(void)time
{
    int page=(int)self.pageControl.currentPage;
    if (page==2) {
        page=0;
    }else
    {
        page++;
    }
    CGFloat x=page*self.scrollview.frame.size.width;
    self.scrollview.contentOffset=CGPointMake(x, 0);
}
#pragma mark--tableView数据源代理方法
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.dataSourceArr.count-3;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    HRReDianCell *cell=[tableView dequeueReusableCellWithIdentifier:CELLID];
    HRReDianModel *model=self.dataSourceArr[indexPath.row+3];
    cell.model=model;
    return cell;
    
}
@end
